package com.espirit.eap.manager.workflow;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.common.Form;
import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.Action;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class WFInstanceTrackerService {
	@Resource
	GenericService genericService;

	/*
	  link:/espirit/eap/manager/workflow/wFInstanceTracker.search.html?newSearch=true&model.parent.srcId=${Parameters["model.id"]!}&model.parent.srcClassName=${Parameters["_type"]!}
	  
	*/
	@Rest(simpleServiceName="loginUser", type="free")
	public PageInfo search(WFInstanceTracker model , int start, int limit,
    		String sort, String dir, boolean newSearch, Reference<QLInfo> qlInfoRef){
//		if (model != null && model.getParent() != null && model.getParent().getId() != null){
//			model.getParent().setSrcClassName(_type);
//		}
		return genericService.search(model, start, limit, sort, dir, newSearch, qlInfoRef);
	}
	
	@Rest(type=Action.SEARCH)
	public PageInfo searchRelatedResult(FormMessage model){
		if (model == null || model.getId() == null) return null;
		
		FormMessage formMessage = genericService.load(FormMessage.class, model.getId());
		Form form = formMessage.getForm();
		
		WFInstanceTracker example = new WFInstanceTracker();
		ModelUtil.setProperty(example, "parent.srcClassName", form.getClass().getName());
		ModelUtil.setProperty(example, "parent.srcId", form.getId());
		return genericService.searchAll(example);
	}
}
